package edu.tms.dao;

import java.util.List;
import java.util.Map;







import edu.tms.bean.ClassFee;
import edu.tms.crud.JdbcUtil;
import edu.tms.dao.common.CommnonDao;

/**
 * 班级财务信息表dao
 * @author Administrator
 *
 */
public class ClassFeeDaoImpl implements CommnonDao<ClassFee>{
	/**
	 * 保存内容
	 * @param value
	 * @return 
	 * @throws Exception
	 */
	@Override
	public long save(ClassFee value) throws Exception {
		String sql="INSERT INTO t_class_fee VALUES(null,?,?,?,?,?,?,?)";
		return JdbcUtil.insertAuto(sql,value.getClassId(),
				value.getUserId(),
				value.getMoney(),
				value.getTotal(),
				value.getDetails(),
				value.getCreateTime(),
				value.getClassFeeState());
	}
	/**
	 * 修改内容
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int update(ClassFee value) throws Exception {
		ClassFee classFee = (ClassFee)value;
		String sql="UPDATE t_class_fee SET class_id=?,total=?,datails=?,create_time=?,";
		sql+="class_fee_state=? WHERE class_fee_id=?";
		int Number=JdbcUtil.update(sql,
				classFee.getClassId(),
				classFee.getTotal(),
				classFee.getDetails(),
				classFee.getCreateTime(),
				classFee.getClassFeeState(),
				classFee.getClassFeeId());
		return Number;
	}
	/**
	 * 删除内容
	 * 班级表 t_class
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int delete(ClassFee value) throws Exception {
		return 0;
	}
	/**
	 * 查找所有的内容
	 * 班级表 t_class
	 * @param pageNo 第几页
	 * @param pageSize 每页数字
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ClassFee> findAll(int pageNo, int pageSize) throws Exception {
		String sql = "SELECT ci.class_name,u.user_name,cf.money,cf.total,cf.details,cf.create_time,cf.class_fee_state ";
		sql += "FROM t_class_info ci,t_user u,t_class_fee cf ";
		sql += "WHERE ci.class_id = cf.class_id AND u.user_id = cf.user_id limit ?,?";
		return JdbcUtil.query(ClassFee.class, sql,(pageNo-1)*pageSize,pageSize);
	}
	/**
	 * String 对应  数据库 列名（comment)
	 * Object 对应相应列名值
	 * 返回的是所查找的类中的数据。方便EXCEL导出
	 */
	@Override
	public List<Map<String,Object>> getMapList() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<ClassFee> findAll(Object key, int pageNo, int pageSize) throws Exception {
		String sql="SELECT tcf.class_id,tc.junior_major_name,tc.class_name,tcf.total,tcf.total,tcf.datails,tcf.create_time,tcf.class_fee_state ";
		sql+="FROM  t_class_fee tcf,t_class tc ";
		sql+="WHERE tcf.class_id=tc.class_id  AND tc.class_name=? OR ";
		sql+="tc.junior_major_name=?  OR  tcf.class_id=?  limit ?,? "; 
		return JdbcUtil.query(ClassFee.class, sql,key,key,key,key,(pageNo-1)*pageSize,pageSize);
	}
	@Override
	public ClassFee findByKey(Object key) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 计算班级总共有多少的财务
	 * @param classId
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	public long getCount(Integer classId,Integer pageSize) throws Exception{
		long count = (Long) JdbcUtil.uniqueValue("select count(*) from t_class_fee where class_id = ?", 1, classId);
		long pages = pageSize.longValue();
		return count%pages==0?count/pages:count/pages+1;
	}
	
	/**
	 * 计算某班一共有多少财务信息
	 * @param classId
	 * @param pageNo
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<ClassFee> findByClassId(Integer classId, int pageNo, int pageSize) throws Exception {
		String sql = "SELECT ci.class_name,u.user_name,cf.money,cf.total,cf.details,cf.create_time,cf.class_fee_state ";
		sql += "FROM t_class_info ci,t_user u,t_class_fee cf ";
		sql += "WHERE ci.class_id = cf.class_id AND u.user_id = cf.user_id and cf.class_id = ? order by cf.class_fee_id desc limit ?,? ";
		return JdbcUtil.query(ClassFee.class, sql,classId,(pageNo-1)*pageSize,pageSize);
	}
	
	
	public Integer getTotal(Integer classId) throws Exception{
		return (Integer) JdbcUtil.uniqueValue("select total from t_class_fee where class_id = ? order by class_fee_id desc limit 0,1", 1, classId);
	}
}
